// Mock Market Data Server
// 提供市场数据仪表盘所需的所有API端点

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());
app.use(express.json());

// Mock 国内指数
app.get('/api/v1/market/indexes', (req, res) => {
  res.json({
    success: true,
    data: [
      {
        code: '000001.SH',
        name: '上证指数',
        current: 3245.67,
        change: 38.45,
        change_pct: 1.23,
        high: 3256.00,
        low: 3201.00,
        volume: 320000000,
        timestamp: new Date().toISOString(),
      },
      {
        code: '399001.SZ',
        name: '深证成指',
        current: 10567.89,
        change: 125.34,
        change_pct: 1.20,
        high: 10589.00,
        low: 10456.00,
        volume: 280000000,
        timestamp: new Date().toISOString(),
      },
      {
        code: '399006.SZ',
        name: '创业板指',
        current: 2145.32,
        change: 28.91,
        change_pct: 1.37,
        high: 2156.00,
        low: 2123.00,
        volume: 150000000,
        timestamp: new Date().toISOString(),
      },
      {
        code: 'HSI',
        name: '恒生指数',
        current: 16234.56,
        change: -123.45,
        change_pct: -0.75,
        high: 16398.00,
        low: 16189.00,
        volume: 98000000,
        timestamp: new Date().toISOString(),
      },
    ],
    timestamp: new Date().toISOString(),
  });
});

// Mock 全球指数
app.get('/api/v1/market/global-indexes', (req, res) => {
  res.json({
    success: true,
    data: {
      americas: [
        {
          code: 'DJI',
          name: '道琼斯工业指数',
          name_en: 'Dow Jones Industrial Average',
          current: 34567.89,
          change: 425.67,
          change_pct: 1.25,
          high: 34789.12,
          low: 34123.45,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'IXIC',
          name: '纳斯达克综合指数',
          name_en: 'NASDAQ Composite',
          current: 13245.32,
          change: 278.45,
          change_pct: 2.15,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'SPX',
          name: '标普500指数',
          name_en: 'S&P 500',
          current: 4432.15,
          change: 65.23,
          change_pct: 1.50,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
      ],
      europe: [
        {
          code: 'FTSE',
          name: '英国富时100',
          name_en: 'FTSE 100',
          current: 7456.23,
          change: -58.34,
          change_pct: -0.78,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'DAX',
          name: '德国DAX30',
          name_en: 'DAX',
          current: 15678.90,
          change: 78.45,
          change_pct: 0.50,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'CAC',
          name: '法国CAC40',
          name_en: 'CAC 40',
          current: 6789.12,
          change: 34.56,
          change_pct: 0.51,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
      ],
      asia: [
        {
          code: 'N225',
          name: '日经225指数',
          name_en: 'Nikkei 225',
          current: 28456.78,
          change: 502.34,
          change_pct: 1.80,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'KOSPI',
          name: '韩国综合指数',
          name_en: 'KOSPI',
          current: 2567.89,
          change: -7.89,
          change_pct: -0.31,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
        {
          code: 'TWII',
          name: '台湾加权指数',
          name_en: 'Taiwan Weighted',
          current: 16234.56,
          change: 123.45,
          change_pct: 0.77,
          market_status: 'closed',
          local_time: new Date().toISOString(),
          timestamp: new Date().toISOString(),
        },
      ],
    },
    timestamp: new Date().toISOString(),
  });
});

// Mock 个股排行
app.get('/api/v1/market/ranking', (req, res) => {
  const { type } = req.query;
  
  const mockStocks = {
    top_gainers: [
      { rank: 1, code: '600519.SH', name: '贵州茅台', current_price: 1850.00, change_pct: 10.00, change_amount: 168.18, volume: 856000, turnover: 8560000000, is_limit_up: true },
      { rank: 2, code: '000858.SZ', name: '五粮液', current_price: 185.60, change_pct: 10.00, change_amount: 16.87, volume: 1234000, turnover: 4560000000, is_limit_up: true },
      { rank: 3, code: '300750.SZ', name: '宁德时代', current_price: 320.45, change_pct: 9.85, change_amount: 28.71, volume: 2345000, turnover: 7890000000 },
      { rank: 4, code: '688981.SH', name: '中芯国际', current_price: 68.90, change_pct: 9.23, change_amount: 5.82, volume: 3456000, turnover: 5670000000 },
      { rank: 5, code: '601888.SH', name: '中国中免', current_price: 125.30, change_pct: 8.76, change_amount: 10.09, volume: 1567000, turnover: 3450000000 },
      { rank: 6, code: '002475.SZ', name: '立讯精密', current_price: 42.15, change_pct: 8.34, change_amount: 3.24, volume: 4567000, turnover: 6780000000 },
      { rank: 7, code: '600036.SH', name: '招商银行', current_price: 45.80, change_pct: 7.92, change_amount: 3.36, volume: 5678000, turnover: 8900000000 },
      { rank: 8, code: '000001.SZ', name: '平安银行', current_price: 15.60, change_pct: 7.58, change_amount: 1.10, volume: 6789000, turnover: 4560000000 },
      { rank: 9, code: '601318.SH', name: '中国平安', current_price: 58.90, change_pct: 7.21, change_amount: 3.96, volume: 4567000, turnover: 7890000000 },
      { rank: 10, code: '600900.SH', name: '长江电力', current_price: 24.30, change_pct: 6.89, change_amount: 1.57, volume: 3456000, turnover: 2340000000 },
    ],
    top_losers: [
      { rank: 1, code: '600001.SH', name: '邯郸钢铁', current_price: 3.20, change_pct: -10.00, change_amount: -0.36, volume: 1234000, turnover: 456000000, is_limit_down: true },
      { rank: 2, code: '000002.SZ', name: '万科A', current_price: 12.50, change_pct: -9.80, change_amount: -1.36, volume: 2345000, turnover: 678000000 },
      { rank: 3, code: '600003.SH', name: '退市股', current_price: 0.80, change_pct: -9.50, change_amount: -0.08, volume: 890000, turnover: 234000000 },
      { rank: 4, code: '000004.SZ', name: '国农科技', current_price: 5.60, change_pct: -8.90, change_amount: -0.55, volume: 1567000, turnover: 345000000 },
      { rank: 5, code: '600005.SH', name: '武钢股份', current_price: 2.45, change_pct: -8.20, change_amount: -0.22, volume: 2345000, turnover: 567000000 },
      { rank: 6, code: '000006.SZ', name: '深振业A', current_price: 4.10, change_pct: -7.80, change_amount: -0.35, volume: 890000, turnover: 234000000 },
      { rank: 7, code: '600006.SH', name: '东风汽车', current_price: 6.70, change_pct: -7.50, change_amount: -0.54, volume: 1234000, turnover: 456000000 },
      { rank: 8, code: '000007.SZ', name: '全新好', current_price: 8.90, change_pct: -7.20, change_amount: -0.69, volume: 1567000, turnover: 678000000 },
      { rank: 9, code: '600007.SH', name: '中国国贸', current_price: 11.20, change_pct: -6.90, change_amount: -0.83, volume: 890000, turnover: 345000000 },
      { rank: 10, code: '000008.SZ', name: '神州高铁', current_price: 3.50, change_pct: -6.70, change_amount: -0.25, volume: 2345000, turnover: 567000000 },
    ],
    most_active: [
      { rank: 1, code: '601398.SH', name: '工商银行', current_price: 5.60, change_pct: 0.36, change_amount: 0.02, volume: 123456789, turnover: 23456789000 },
      { rank: 2, code: '601939.SH', name: '建设银行', current_price: 6.80, change_pct: 0.45, change_amount: 0.03, volume: 98765432, turnover: 19876543000 },
      { rank: 3, code: '601288.SH', name: '农业银行', current_price: 3.45, change_pct: 0.29, change_amount: 0.01, volume: 87654321, turnover: 15678900000 },
      { rank: 4, code: '601988.SH', name: '中国银行', current_price: 3.60, change_pct: 0.28, change_amount: 0.01, volume: 76543210, turnover: 14567890000 },
      { rank: 5, code: '601328.SH', name: '交通银行', current_price: 5.20, change_pct: 0.39, change_amount: 0.02, volume: 65432109, turnover: 13456789000 },
      { rank: 6, code: '000001.SZ', name: '平安银行', current_price: 15.60, change_pct: 1.30, change_amount: 0.20, volume: 54321098, turnover: 12345678000 },
      { rank: 7, code: '600036.SH', name: '招商银行', current_price: 45.80, change_pct: 0.88, change_amount: 0.40, volume: 43210987, turnover: 11234567000 },
      { rank: 8, code: '601166.SH', name: '兴业银行', current_price: 18.90, change_pct: 0.53, change_amount: 0.10, volume: 32109876, turnover: 10123456000 },
      { rank: 9, code: '600016.SH', name: '民生银行', current_price: 4.20, change_pct: 0.24, change_amount: 0.01, volume: 21098765, turnover: 9012345000 },
      { rank: 10, code: '600000.SH', name: '浦发银行', current_price: 8.90, change_pct: 0.34, change_amount: 0.03, volume: 10987654, turnover: 8901234000 },
    ],
  };

  res.json({
    success: true,
    data: {
      type: type || 'top_gainers',
      market: 'all',
      stocks: mockStocks[type] || mockStocks.top_gainers,
      total: 10,
    },
    timestamp: new Date().toISOString(),
  });
});

// Mock K线数据
app.get('/api/v1/market/index-bars', (req, res) => {
  const bars = [];
  const now = new Date();
  const startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 9, 30);
  
  // 生成48个5分钟K线（4小时交易时间）
  for (let i = 0; i < 48; i++) {
    const time = new Date(startTime.getTime() + i * 5 * 60 * 1000);
    const base = 3200 + Math.random() * 100;
    const open = base;
    const close = base + (Math.random() - 0.5) * 30;
    const high = Math.max(open, close) + Math.random() * 20;
    const low = Math.min(open, close) - Math.random() * 20;
    
    bars.push({
      timestamp: time.toISOString(),
      open: parseFloat(open.toFixed(2)),
      high: parseFloat(high.toFixed(2)),
      low: parseFloat(low.toFixed(2)),
      close: parseFloat(close.toFixed(2)),
      volume: Math.floor(Math.random() * 20000000) + 5000000,
      amount: Math.random() * 10000000000,
    });
  }

  res.json({
    success: true,
    data: {
      code: req.query.code || '000001.SH',
      name: '上证指数',
      period: req.query.period || '5m',
      bars,
      total: bars.length,
    },
    timestamp: new Date().toISOString(),
  });
});

const PORT = 8000;
app.listen(PORT, () => {
  console.log(`🚀 Mock Market Data Server is running!`);
  console.log(`📍 URL: http://localhost:${PORT}`);
  console.log(`\n✅ Available Endpoints:`);
  console.log(`   GET /api/v1/market/indexes`);
  console.log(`   GET /api/v1/market/global-indexes`);
  console.log(`   GET /api/v1/market/ranking`);
  console.log(`   GET /api/v1/market/index-bars`);
  console.log(`\n💡 Open http://localhost:3001/market to view the dashboard`);
});


